<?php
class UsersController extends AppController {
	public $uses = array('User');
	
	public function beforeFilter() {
		$this->Auth->allow('register');
		$this->Auth->authenticate = array(
			'Form' => array(
				'userModel' => 'User',
				'fields' => array('username' => 'user_name', 'password' => 'password')
			)
		);
		
		$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
		$this->Auth->loginRedirect = array('controller' => 'home', 'action' => 'index');
		$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
		$this->Auth->loginError = 'Failed to login';
		$this->Auth->authError = 'Access denied';
	}
	
	public function register() {
		if ($this->request->is('post')) {
			$data = $this->request->data;
			
			$validateOk = true;
			
			// Kiem tra user_name co trung khong ?
			$checkUserName = $this->User->findByUserName($data['User']['user_name']);
			
			if (!empty($checkUserName)) {
				$validateOk = false;
				$this->Session->setFlash('This user_name is existed.');
			}
			
			// Kiem tra email co ton tai khong
			$checkEmail = $this->User->findByEmail($data['User']['email']);
			
			if (!empty($checkEmail)) {
				$validateOk = false;
				$this->Session->setFlash('This Email is existed.');
			}
			
			// Kiem tra password va confirm_password
			if ($data['User']['password'] != $data['User']['confirm_password']) {
				$validateOk = false;
				$this->Session->setFlash('Password and Confirm password do not matched.');
			}
			
			if ($validateOk) {
				unset($data['User']['confirm_password']);
				$data['User']['password'] = md5($data['User']['password']);
				$data['User']['create_time'] = date('Y-m-d H:i:s');
				
				$this->User->create();
				if ($this->User->save($data['User'])) {
					$this->Session->setFlash('Account has been created !', 'success');
					$this->redirect(array('controller' => 'users', 'action' => 'login'));
				} else {
					$this->Session->setFlash('Account cannot create. Please try again !');
				}
			}
		}
		
		$this->set('title_for_content', 'Sign Up');
	}
	
	public function login() {
		if ($this->Auth->loggedIn()) {
			// Truong hop login roi thi khong cho phep vao trang nay
			$this->redirect(array('controller' => 'home', 'action' => 'index'));
		}
		
		if ($this->request->is('post')) {
			$userLoginInfo = $this->request->data['User'];
			
			$userLoginInfo['password'] = md5($userLoginInfo['password']);
			
			// Tim thong tin trong bang UserMaster
			$userInfo = $this->User->find('first', array(
				'conditions' => array(
					'user_name' => $userLoginInfo['user_name'],
					'password' => $userLoginInfo['password'],
					'delete_flag' => 0
					),
				'contain' => false
				));
				
			if (!empty($userInfo)) {
				if ($this->Auth->login($userInfo['User'])) {
					// Truong hop login thanh cong
					$this->redirect(array('controller' => 'home', 'action' => 'index'));
				} else {
					// Truong hop login that bai
					$this->Session->setFlash('Login failed !!!');
				}
			} else {
				$this->Session->setFlash('Login failed !!!');
			}			
		}
		
		$this->set('title_for_content', 'Login');
	}
	
	public function logout() {
    	$this->Auth->logout();
    	$this->Session->destroy();
    	$this->redirect(array('controller' => 'users', 'action' => 'login'));
    }

}